<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">

      <el-form-item label="申请理由:">
        <el-input v-model="queryParams.applyReason" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>


      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermission="['baoli:finCustomerCreditApplication:insert']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['baoli:finCustomerCreditApplication:update']" type="success" icon="el-icon-edit" size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['baoli:finCustomerCreditApplication:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['baoli:finCustomerCreditApplication:insert']" type="primary" icon="el-icon-upload2" size="mini" :disabled="isSingle" @click="openUploadDialog">上传申请文件</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['baoli:finCustomerCreditApplication:export']" type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="申请原因" align="center" prop="applyReason" show-overflow-tooltip />
      <el-table-column label="申请信用额" width="155" align="center" prop="needCredit" show-overflow-tooltip />
      <el-table-column label="申请协议" align="center" show-overflow-tooltip >
        <template slot-scope="scope">
          <a @click.prevent="handleDownload(scope.row.applyAttachmentUrl)">{{ scope.row.applyAttachmentUrl }}</a>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="proveStatus" show-overflow-tooltip >
        <template slot-scope="scope">
          <span>{{ scope.row.proveStatus | proveStatusFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
           <el-button v-hasPermission="['baoli:finCustomerCreditApplication:platFormprove']" size="mini" type="text" icon="el-icon-edit" @click="handleProve(scope.row)">查看-审核</el-button>
          <el-button v-hasPermission="['baoli:finCustomerCreditApplication:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button v-hasPermission="['baoli:finCustomerCreditApplication:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!-- 添加/修改对话框 -->
    <el-dialog :title="finCustomerCreditApplicationFormDialog.title" :visible.sync="finCustomerCreditApplicationFormDialog.visible" :close-on-click-modal="false" :before-close="resetFinCustomerCreditApplicationFormDialog" width="500px" append-to-body>
      <el-form ref="finCustomerCreditApplicationForm" :model="finCustomerCreditApplicationFormData" :rules="finCustomerCreditApplicationFormRules" label-width="80px">

        <el-form-item label="申请理由" prop="applyReason">
          <el-input v-model="finCustomerCreditApplicationFormData.applyReason" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="信用额" prop="needCredit">
          <el-input v-model="finCustomerCreditApplicationFormData.needCredit" placeholder="请输入" />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="finCustomerCreditApplicationFormData.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmFinCustomerCreditApplicationFormDialog">确 定</el-button>
        <el-button @click="resetFinCustomerCreditApplicationFormDialog">取 消</el-button>
      </div>
    </el-dialog>
    <div class="dialog_container">
      <el-dialog :title="uploadDialog.title" :visible.sync="uploadDialog.visible" width="400px" append-to-body>
        <div>
          <el-upload
            ref="upload"
            :limit="1"
            :multiple="false"
            :headers="uploadData.headers"
            :action="uploadData.url"
            :disabled="uploadData.isUploading"
            :on-progress="handleFileUploadProgress"
            :on-success="handleFileSuccess"
            :on-error="handleFileError"
            :auto-upload="true"
            :show-file-list="true"
            drag
          >
            <i class="el-icon-upload" />
            <div class="el-upload__text">
              将文件拖到此处，或
              <em>点击上传</em>
            </div>
          </el-upload>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="confirmUploadDialog">确 定</el-button>
            <el-button @click="resetUploadDialog">取 消</el-button>
          </div>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import FinCustomerCreditApplicationApi from '@/api/baoli/FinCustomerCreditApplicationApi'
import appConstant from '@/constant/AppConstant'
import AuthUtils from '@/utils/AuthUtils'
const defaultFinCustomerCreditApplicationFormData = {
  applyReason: null,
  needCredit: null,
  applyAttachmentUrl: null,
  proveStatus: '',

}
export default {
  name: 'FinCustomerCreditApplicationList',
  filters: {
    proveStatusFilter(item) {
      const values = appConstant.creditProveStatusList.filter(value => value.key === item)
      if (values && values.length > 0) {
        return values[0].label
      } else {
        return ''
      }
    }
  },
  data() {
    return {
      options: {
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        applyReason: null,
        needCredit: null,
        applyAttachmentUrl: null,
        proveStatus:  'applied',
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      finCustomerCreditApplicationFormDialog: {
        title: '',
        visible: false
      },
      uploadDialog: {
        title: '附件上传',
        visible: false
      },
      uploadData: {
        headers: { Authorization: 'Bearer ' + AuthUtils.getToken() }, // 上传请求头
        url: process.env.VUE_APP_BASE_API + '/system/attachment/upload' // 上传的地址
      },
      finCustomerCreditApplicationFormData: Object.assign({}, defaultFinCustomerCreditApplicationFormData),
      finCustomerCreditApplicationFormRules: {
      },
      attach: {
        id: null,
        applyAttachmentUrl: null
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
    },
    handleAdd() {
      this.openFinCustomerCreditApplicationFormDialog()
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return FinCustomerCreditApplicationApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.openFinCustomerCreditApplicationFormDialog(id)
    },

    handleProve(row) {
      this.$router.push({ path: `/finCustomerCreditApplication/finCustomerCreditApplicationView/${row.id}` })
    },
    handleQuery() {
      this.loading = true
      FinCustomerCreditApplicationApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return FinCustomerCreditApplicationApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.msg)
      })
    },
    handleDownload(applyAttachmentUrl) {
      if (applyAttachmentUrl) {
        this.$common.download(process.env.VUE_APP_ATTACHMENT_URL + '/' + applyAttachmentUrl)
      }
    },
    openFinCustomerCreditApplicationFormDialog(id) {
      this.finCustomerCreditApplicationFormDialog.visible = true
      if (id) {
        this.finCustomerCreditApplicationFormDialog.title = '客户授信额度申请列-修改'
        FinCustomerCreditApplicationApi.get(id).then(response => {
          this.finCustomerCreditApplicationFormData = {
            ...this.finCustomerCreditApplicationFormData,
            ...response.data
          }
        })
      } else {
        this.finCustomerCreditApplicationFormDialog.title = '客户授信额度申请列-添加'
      }
    },
    confirmFinCustomerCreditApplicationFormDialog() {
      this.$refs['finCustomerCreditApplicationForm'].validate(valid => {
        if (valid) {
          if (this.finCustomerCreditApplicationFormData.id) {
            FinCustomerCreditApplicationApi.edit(this.finCustomerCreditApplicationFormData).then(response => {
              this.$message.success('修改成功')
              this.resetFinCustomerCreditApplicationFormDialog()
              this.handleQuery()
            })
          } else {
            FinCustomerCreditApplicationApi.add(this.finCustomerCreditApplicationFormData).then(response => {
              this.$message.success('添加成功')
              this.resetFinCustomerCreditApplicationFormDialog()
              this.handleQuery()
            })
          }
        }
      })
    },
    resetFinCustomerCreditApplicationFormDialog() {
      this.finCustomerCreditApplicationFormDialog = {
        title: '',
        visible: false
      }
      this.finCustomerCreditApplicationFormData = Object.assign({}, defaultFinCustomerCreditApplicationFormData)
      this.resetForm('finCustomerCreditApplicationForm')
    },
    openUploadDialog(row) {
      this.resetUploadDialog()
      this.uploadDialog.visible = true
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.attach.id = id
    },
    confirmUploadDialog() {
      FinCustomerCreditApplicationApi.edit(this.attach).then(response => {
        this.$message.success('附件添加成功')
        this.uploadDialog.visible = false
        this.handleQuery()
      })
    },
    resetUploadDialog() {
      this.uploadDialog.visible = false
      this.attach = {
        id: null,
        applyAttachmentUrl: null
      }
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.uploadData.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.uploadData.isUploading = false
      if (response.code === 200) {
        this.attach.applyAttachmentUrl = response.data[0].fileUrl
      } else {
        fileList.pop()
        this.$message.error(response.msg)
      }
    },
    // 文件上传失败处理
    handleFileError(response, file, fileList) {
      this.uploadData.isUploading = false
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
